package com.cunshe.web.manage;

import com.cunshe.common.ResponseVO;
import com.cunshe.config.annotation.Login;
import com.cunshe.domain.Product;
import com.cunshe.domain.Video;
import com.cunshe.service.ProductService;
import com.cunshe.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频
 *
 * @author xiao
 */
@Api(tags = "视频")
@Login
@RestController
@RequestMapping(value = "/manage/video")
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private ProductService productService;

    /**
     * 新增
     */
    @ApiOperation(value = "新增")
    @PostMapping(value = "insert")
    public ResponseVO insert(@RequestBody Video record) {
        videoService.insert(record);
        return ResponseVO.success();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    public ResponseVO delete(@RequestParam("id") Long id) {
        videoService.delete(id);
        return ResponseVO.success();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public ResponseVO update(@RequestBody Video record) {
        videoService.update(record);
        return ResponseVO.success();
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping(value = "get")
    public ResponseVO get(@RequestParam("id") Long id) {
        Video video = videoService.get(id);
        return ResponseVO.success(video);
    }

    /**
     * 查询列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping(value = "list")
    public ResponseVO list(@RequestBody Video record) {
        List<Video> videoList = videoService.list(record);
        List<Product> productList = productService.list(null);
        if(!CollectionUtils.isEmpty(productList)){
            for (Video video : videoList) {
                if(video.getProductId() != null && video.getProductId() != 0){
                    Product product = productList.stream()
                            .filter(var -> var.getId().equals(video.getProductId()))
                            .findFirst()
                            .orElse(null);
                    if(product != null){
                        video.setProductName(product.getTitle());
                    }
                }

            }
        }

        return ResponseVO.success(videoList);
    }

}